package com.right.health.app.ui.household;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.BloodPressureResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.HouseholdApi;
import com.right.health.app.ui.household.adapter.BloodPressureListAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 *  血压
 *
 * Created by hopeliao on 2019-10-02
 *
 */
public class BloodPressureListActivity extends BaseActivity implements  OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private BloodPressureListAdapter mAdapter;

    private HouseholdApi mHouseholdApi;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo;

    private List<BloodPressureResponse.BloodPressureInfo> mDataSource = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.blood_pressure_list);
    }

    @Override
    public void findView() {

        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BloodPressureResponse.BloodPressureInfo info = (BloodPressureResponse.BloodPressureInfo)parent.getItemAtPosition(position);

                if(info != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString(BroadCastKey.DATA, info.medicalrecordid);
                    startIntent(BloodPressureDetailActivity.class, bundle);
                }

            }
        });

        mAdapter = new BloodPressureListAdapter(this);
        listView.setAdapter(mAdapter);
    }

    @Override
    public void initialize() {
        mHouseholdApi = new HouseholdApi();
        mHouseholdApi.setListener(this);

        onReceiveBroadcast(BroadCastCode.REFRESH_COMMON, null);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if(intent == BroadCastCode.REFRESH_COMMON) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(response.targetData instanceof BloodPressureResponse) {
                BloodPressureResponse bloodPressureResponse = response.cast(BloodPressureResponse.class);

                if(request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }
                mDataSource.addAll(bloodPressureResponse.bloodpressures);

                if (bloodPressureResponse.page != null) {
                    mRefreshView.setCanLoadMore(!bloodPressureResponse.page.lastPage);
                }
                mAdapter.setDataSource(mDataSource);
            }
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;

        mHouseholdApi.addRequestCode(HTTP_CODE_REFRESH);
        mHouseholdApi.getBloodPressureList(mPageNo);
    }

    @Override
    public void onLastItemVisible() {
        mPageNo += 1;
        mHouseholdApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mHouseholdApi.getBloodPressureList(mPageNo);
    }
}
